package service.impl;

import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import pojo.Appuser;
import pojo.Watchuser;
import service.UserService;
import dao.UserDao;
import daoImpl.UserDaoImpl;



public class UserServiceImpl implements UserService {
	private UserDao userDao = new UserDaoImpl();
	

	public UserDao getUserDao() {
		return userDao;
	}


	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}


	@Override
	public Appuser login(Appuser appuser) {

	return  userDao.login(appuser);
	
	
	}


	@Override
	public boolean register(Appuser appuser) {

		String username = appuser.getUsername();
		if(userDao.queryByUsername(username)!=null){//如果不为空，说明用户名已经存在
			return false;
		}else{
			//保存新注册的用户
			userDao.save(appuser);
			return true;
		}
	
	}

	/*
	 * 绑定智能手表用户
	 * 一个监护人的手机号只能绑定一个被监护人手机号
	 * @see service.UserService#bind(pojo.Watchuser)
	 */
	@Override
	public boolean bind(Watchuser watchuser) {
		Appuser appuser = new Appuser();
		String username = watchuser.getUsername();
		String binder = watchuser.getBinder();
		//如果前台传递过来的监护人和被监护人手机号码都为空，返回false
		if(username==null || "".equals(username.trim()) || binder==null || "".equals(binder.trim())){
			return false;
		}
		//查询数据库中watchuser是否已经存在监护人的手机号，如果已经存在，说明之前已经绑定一个手机号了，返回false
		
		if(userDao.query(binder)!=null){
			return false;
		}else{
			//绑定智能手表
			 watchuser = userDao.bind(watchuser);
			 if(watchuser==null){
				 return false;
				 
			 }else{
				 return true;
			 }
		
		
		}

	}


	/* 解除绑定
	 * @see service.UserService#dele(pojo.Watchuser)
	 */
	@Override
	public boolean dele(String username) {
		//String username = watchuser.getUsername();
		if(username==null){
			return false;
		}
		if(userDao.queryUsername(username)==null){
			return false;
		}else{
			userDao.dele(username);
			return true;
		}
		
	}


	/* 
	 * @see service.UserService#queryList(java.lang.String)
	 */
	@Override
	public List<Watchuser> queryList(String binder) {
		return  userDao.queryList(binder);
	}


	/* (non-Javadoc)
	 * @see service.UserService#update(pojo.Appuser)
	 */
	@Override
	public Appuser update(Appuser appuser) {
		return  userDao.update(appuser);
	
	}

	
}
